function AMapWX(a) {
  this.key = a.key;
  this.requestConfig = {
    key: a.key,
    s: "rsx",
    platform: "WXJS",
    appname: a.key,
    sdkversion: "1.2.0",
    logversion: "2.0"
  };
}

AMapWX.prototype.getWxLocation = function(a, b) {
  wx.getLocation({
    type: "gcj02",
    success: function(c) {
      var location = c.longitude + "," + c.latitude;
      b(location);
    },
    fail: function(c) {
      wx.showModal({
        title: "提示",
        content: "获取位置失败，请检查是否开启定位权限",
        showCancel: false
      });
      a.fail({
        errCode: "0",
        errMsg: c.errMsg || ""
      });
    }
  });
};

AMapWX.prototype.getRegeo = function(a) {
  var c = this;
  function b(d) {
    var e = c.requestConfig;
    wx.request({
      url: "https://restapi.amap.com/v3/geocode/regeo",
      data: {
        key: c.key,
        location: d,
        extensions: "all",
        s: e.s,
        platform: e.platform,
        appname: c.key,
        sdkversion: e.sdkversion,
        logversion: e.logversion
      },
      method: "GET",
      header: {
        "content-type": "application/json"
      },
      success: function(f) {
        var g = f.data;
        if (g.status && "1" == g.status) {
          f = g.regeocode;
          if (f) {
            d = d.split(",");
            g = {};
            var h = f.addressComponent,
              i = f.roads[0].name + "附近",
              j = d[0],
              k = d[1];
            g.formatted_address = f.formatted_address;
            g.address = i;
            g.province = h.province;
            g.city = h.city;
            g.district = h.district;
            g.township = h.township;
            g.street = h.street;
            g.street_number = h.streetNumber;
            g.country = h.country;
            g.adcode = h.adcode;
            g.citycode = h.citycode;
            g.latitude = k;
            g.longitude = j;
            a.success(g);
          }
        } else a.fail({
          errCode: g.infocode,
          errMsg: g.info
        });
      },
      fail: function(f) {
        a.fail({
          errCode: "0",
          errMsg: f.errMsg || ""
        });
      }
    });
  }
  
  a.location ? b(a.location) : c.getWxLocation(a, function(d) {
    b(d);
  });
};

AMapWX.prototype.getPoiAround = function(a) {
  var c = this,
    d = c.requestConfig;
    
  function b(e) {
    var f = {
      key: c.key,
      location: e,
      s: d.s,
      platform: d.platform,
      appname: c.key,
      sdkversion: d.sdkversion,
      logversion: d.logversion
    };
    a.querytypes && (f.types = a.querytypes);
    a.querykeywords && (f.keywords = a.querykeywords);
    a.radius && (f.radius = a.radius);
    a.page && (f.page = a.page);
    a.offset && (f.offset = a.offset);
    
    wx.request({
      url: "https://restapi.amap.com/v3/place/around",
      data: f,
      method: "GET",
      header: {
        "content-type": "application/json"
      },
      success: function(g) {
        if (g.data.status && "1" == g.data.status) {
          if ((g = g.data) && g.pois) {
            var h = {
              markers: [],
              poisData: g.pois
            };
            for (var i = 0; i < g.pois.length; i++) {
              var j = {
                id: i,
                latitude: parseFloat(g.pois[i].location.split(",")[1]),
                longitude: parseFloat(g.pois[i].location.split(",")[0]),
                title: g.pois[i].name,
                address: g.pois[i].address,
                distance: g.pois[i].distance
              };
              h.markers.push(j);
            }
            a.success(h);
          }
        } else a.fail({
          errCode: g.data.infocode,
          errMsg: g.data.info
        });
      },
      fail: function(g) {
        a.fail({
          errCode: "0",
          errMsg: g.errMsg || ""
        });
      }
    });
  }
  
  a.location ? b(a.location) : c.getWxLocation(a, function(e) {
    b(e);
  });
};

AMapWX.prototype.getDrivingRoute = function(a) {
  var b = Object.assign({}, this.requestConfig);
  a.origin && (b.origin = a.origin);
  a.destination && (b.destination = a.destination);
  a.strategy && (b.strategy = a.strategy);
  a.waypoints && (b.waypoints = a.waypoints);
  a.avoidpolygons && (b.avoidpolygons = a.avoidpolygons);
  a.avoidroad && (b.avoidroad = a.avoidroad);
  wx.request({
    url: "https://restapi.amap.com/v3/direction/driving",
    data: b,
    method: "GET",
    header: {
      "content-type": "application/json"
    },
    success: function(c) {
      c && c.data && c.data.route && a.success({
        paths: c.data.route.paths,
        taxi_cost: c.data.route.taxi_cost || ""
      });
    },
    fail: function(c) {
      a.fail({
        errCode: "0",
        errMsg: c.errMsg || ""
      });
    }
  });
};

AMapWX.prototype.getWalkingRoute = function(a) {
  var b = Object.assign({}, this.requestConfig);
  a.origin && (b.origin = a.origin);
  a.destination && (b.destination = a.destination);
  wx.request({
    url: "https://restapi.amap.com/v3/direction/walking",
    data: b,
    method: "GET",
    header: {
      "content-type": "application/json"
    },
    success: function(c) {
      c && c.data && c.data.route && a.success({
        paths: c.data.route.paths
      });
    },
    fail: function(c) {
      a.fail({
        errCode: "0",
        errMsg: c.errMsg || ""
      });
    }
  });
};

module.exports = AMapWX; 